list gMenu=[];
list statesMenu=[0,0,0,0];
list gAvatarAllow;
key ToucherID;
integer gMenuLink =1337;
integer gDemon =1338;
integer gPlugin =1339;
integer gVic =1329;
integer channel_dialog;
integer listen_id;
integer listen_id2;
string msg;
float gdistance;
integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
integer crossing=0;
integer random;
vector gColor;
string gMessage="Puppy";
integer sideScript = 1234;
integer gPage;
//

list pageMenu(list in ,integer page)
{
	list out;
	integer maxpages =llGetListLength(in)/9;
	page = (page)%(maxpages+1);
	out =["<<","Main",">>"];
	integer _listcount =(page *9)+9;
	integer _i=_listcount-9;
	
	for (_i;_i<_listcount;_i++)
	{
		if(_i<=llGetListLength(in)&&llList2String(in,_i)!="")
		out+=llList2String(in,_i);
	}
	return out;
}

BuildElementList(string name, string changes)
{
    integer n;
    integer linkcount = llGetNumberOfPrims();

    //root prim is 1, so start at 2
    for (n = 2; n <= linkcount; n++)
    {
        string desc = (string)llGetObjectDetails(llGetLinkKey(n), [OBJECT_NAME]);
        list Tname = llParseString2List(desc,["~"],["###"]);
    	string _Tname =llList2String(Tname,0);
    	llSetLinkAlpha(n, 1.0, ALL_SIDES);
    }
}

makeMenu()
{
	integer n;
    integer linkcount = llGetNumberOfPrims();
    gMenu=[];
    //root prim is 1, so start at 2
    for (n = 2; n <= linkcount; n++)
    {
    	string desc = (string)llGetObjectDetails(llGetLinkKey(n), [OBJECT_NAME]);

    	gMenu+=desc;
    	//list name = llParseString2List(desc,["~"],["###"]);
    	//string _name =llList2String(name,0);
    	/*if(~llListFindList(gMenu, (list)desc))
    	{
    	}
    	else
    	{	
    		
        	
    	}*/
    }
}



displayMenu()
{
	
	list tempMain=[];
	msg = "Pick one";
	if(llGetListLength(gMenu)<10)
	{
		tempMain= [" ","Back", " "]+tempMain;
	}
	else
	{
		tempMain+=gMenu;
		tempMain= pageMenu(tempMain,gPage);
	}
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,tempMain, channel_dialog);
	llSetTimerEvent(45.0);
}





llLRP(integer element_new, integer placeinlist)
{
	statesMenu = llListReplaceList(statesMenu, [element_new], placeinlist, placeinlist);
}

default {
    state_entry() 
    {
    	makeMenu();
    }
     listen(integer channel, string name, key id, string message)
    {
    	if(channel==channel_dialog)
    	{
			
			if(message=="Back")
			{
				llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_MISC", ToucherID);
			}
			if(message=="<<")
	  		{
	  			gPage--;
				integer maxpages =llGetListLength(gMenu)/9;
				if(gPage<0)
				{
					gPage = maxpages;
				}
				displayMenu();
	  		}
	  		else if(message==">>")
	  		{
	  			gPage++;
	  			displayMenu();
	  		}
    	}
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				displayMenu();
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);
  			}
  		}
  	}
  	
  	on_rez(integer num)
    {
        makeMenu();     
    }
  	

  	
  	timer()
  	{
  		llSetTimerEvent(0.0);
  		llListenRemove(listen_id);
  	}
}
